﻿using BizControl.Library.BusinessObjects;
using Csla.Xaml;
using BizControl.Presentation.Infrastructure.Controls;
using Microsoft.Practices.Prism.Regions;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism;
using System;
using BizControl.Presentation.Infrastructure.Constants;
using System.Windows.Input;
using BizControl.Library.BusinessObjects.Marks;

namespace BizControl.Presentation.PersonMarksModule.ViewModels
{
  [Export]
  public class PersonMarksViewModel : ViewModel<PersonMarkList>, INavigationAware, IRegionMemberLifetime, IPartImportsSatisfiedNotification
  {
    private readonly DelegateCommand<Guid?> _editPersonMarkCommand;
    private readonly DelegateCommand _refreshListCommand;
    private readonly DelegateCommand _addPersonMarkCommand;

    private IRegionManager _regionManager;

    [ImportingConstructor]
    public PersonMarksViewModel(IRegionManager regionManager)
    {
      _regionManager = regionManager;
      _editPersonMarkCommand = new DelegateCommand<Guid?>(EditPersonMark);
      _refreshListCommand = new DelegateCommand(RefreshList);
      _addPersonMarkCommand = new DelegateCommand(AddPersonMark);
    }

    #region Commands

    public ICommand EditPersonMarkCommand
    {
      get { return this._editPersonMarkCommand; }
    }

    public ICommand AddPersonMarkCommand
    {
      get { return _addPersonMarkCommand; }
    }

    public ICommand RefreshListCommand
    {
      get { return this._refreshListCommand; }
    }

    private void RefreshList()
    {
      DoRefresh(GetList);
    }

    private void AddPersonMark()
    {
      var uriQuery = new UriQuery();
      uriQuery.Add(NavigationParametersNames.NewPersonMarkName, NavigationParametersNames.CreateNewPersonMark);
      var uri = new Uri(ViewNames.PersonMarkEditViewName + uriQuery.ToString(), UriKind.Relative);
      _regionManager.RequestNavigate(ShellRegionNames.MainContentRegion, uri);
    }

    private void EditPersonMark(Guid? markId)
    {
      if (!string.IsNullOrEmpty(markId.ToString()))
      {
        var uriQuery = new UriQuery();
        uriQuery.Add(NavigationParametersNames.IdName, markId.ToString());
        var uri = new Uri(ViewNames.PersonMarkEditViewName + uriQuery.ToString(), UriKind.Relative);
        this._regionManager.RequestNavigate(ShellRegionNames.MainContentRegion, uri);
      }
    }

    #endregion

    PersonMarkList GetList()
    {
      return PersonMarkList.GetPersonMarkList();
    }

    #region INavigationAware Members

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
      return true;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {
    }

    public void OnNavigatedTo(NavigationContext navigationContext)
    {
      RefreshList();
    }

    #endregion

    #region IRegionMemberLifetime Members

    public bool KeepAlive
    {
      get { return true; }
    }

    #endregion

    #region IPartImportsSatisfiedNotification Members

    public void OnImportsSatisfied()
    {
    }

    #endregion
  }
}
